package cn.explink.dop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.explink.dop.entity.vo.RestaurantVO;
import cn.explink.dop.enums.pub.EnableEnum;
import cn.explink.dop.service.RestaurantService;
import cn.explink.eap.core.common.model.json.AjaxJson;
import cn.explink.eap.web.cgform.controller.autolist.CgAutoListController;

/**
 * 餐厅管理控制器
 *
 * @author firefly 2015-2-26 13:27
 *
 */
@Controller
@RequestMapping("/restaurantController")
public class RestaurantController extends CgAutoListController {

	@Autowired
	private RestaurantService restaurantService;

	/**
	 * 首页面
	 *
	 * @param request
	 *            请求参数
	 * @return 详细页面
	 */
	@RequestMapping(params = "index")
	public ModelAndView resIndex(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		view.setViewName("dop/restaurant/restaurant");
		return view;
	}

	/**
	 * 添加数据
	 *
	 * @param resName
	 *            餐厅名
	 * @param request
	 *            请求参数
	 * @return 添加成功对象
	 */
	@RequestMapping(params = "create")
	@ResponseBody
	public AjaxJson saveBaseInfo(String resName, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			RestaurantVO res = new RestaurantVO();
			res.setResName(resName.trim());
			res.setResEnable(EnableEnum.Enable.getValue());
			String id = this.restaurantService.add(res);
			if (null != id) {
				j.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 修改餐厅信息
	 *
	 * @param baseId
	 * @param baseAreaName
	 * @param baseCoordinate
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "modify")
	@ResponseBody
	public AjaxJson updateData(String id, String resName, String resDesc, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			RestaurantVO res = new RestaurantVO();
			res.setId(id);
			res.setResName(resName);
			res.setResDesc(resDesc);
			this.restaurantService.modify(res);
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 加载本页数据
	 *
	 * @param pageNum
	 *            页码
	 * @param pageSize
	 *            当页条数
	 * @param request
	 *            请求参数
	 * @return 查询结果集
	 */
	@RequestMapping(params = "loadData")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<RestaurantVO> loadData(int pageNum, int pageSize, HttpServletRequest request) {
		try {
			Map<String, Object> map = this.restaurantService.getListDataByPage(new RestaurantVO(), pageNum, pageSize);
			return (List<RestaurantVO>) map.get("list");
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<RestaurantVO>();
		}
	}

	/**
	 * 获取查询数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "queryList")
	@ResponseBody
	public Map<String, Object> queryData(String resName, int pageNum, int pageSize, HttpServletRequest request) {
		try {
			RestaurantVO res = new RestaurantVO();
			if (StringUtils.isNotBlank(resName)) {
				res.setResName(resName);
			}
			return this.restaurantService.getListDataByPage(res, pageNum, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return new HashMap<String, Object>();
		}
	}

	/**
	 * 获取数据
	 *
	 * @param pageNum
	 *            当前页
	 * @param pageSize
	 *            当前页条数
	 * @param request
	 *            请求
	 * @return 结果集
	 */
	@RequestMapping(params = "findData")
	@ResponseBody
	public List<RestaurantVO> findData(HttpServletRequest request) {
		try {
			List<RestaurantVO> list = this.restaurantService.getListData(new RestaurantVO());
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<RestaurantVO>();
		}
	}

	/**
	 *
	 * @Title: stopRows
	 * @Description: 停用餐厅
	 * @author 孟凡岭
	 * @param @param rowList
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "stop")
	@ResponseBody
	public List<Map<String, Object>> stopRows(String rowList, HttpServletRequest request) {
		List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
		try {
			JSONArray json = JSONArray.fromObject(rowList);
			List<RestaurantVO> list = JSONArray.toList(json, RestaurantVO.class);
			map = this.restaurantService.stopRows(list);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return map;
		}
		return map;
	}

	/**
	 *
	 * @Title: openRows
	 * @Description: 启用餐厅
	 * @author 孟凡岭
	 * @param @param rowList
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "open")
	@ResponseBody
	public AjaxJson openRows(String rowList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			JSONArray json = JSONArray.fromObject(rowList);
			List<RestaurantVO> list = JSONArray.toList(json, RestaurantVO.class);
			this.restaurantService.openRows(list);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setSuccess(false);
		}
		return j;
	}

	/**
	 * 删除数据
	 *
	 * @param rowList
	 *            数据字符串
	 * @param request
	 *            求情
	 * @return 删除状态
	 */
	@RequestMapping(params = "delete")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public AjaxJson deleteRows(String rowList, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			JSONArray json = JSONArray.fromObject(rowList);
			List<RestaurantVO> list = JSONArray.toList(json, RestaurantVO.class);
			int num = this.restaurantService.deleteData(list);
			if (num == 0) {
				j.setSuccess(false);
			} else {
				j.setSuccess(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 *
	 * @Title: checkResName
	 * @Description: 判断餐厅是否已经存在
	 * @author 孟凡岭
	 * @param @param resName
	 * @param @param request
	 * @param @return
	 * @return AjaxJson 返回类型
	 * @throws
	 */
	@RequestMapping(params = "checkResName")
	@ResponseBody
	public AjaxJson checkResName(String resName, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		boolean flag = this.restaurantService.checkResName(resName);
		j.setSuccess(flag);
		return j;
	}
}
